﻿# Simple ILMerge PowerShell Script
# Used to merge MouseKeyboardOperationSimulator.exe and Newtonsoft.Json.dll into a single exe file
# Compatible with Windows 7 and .NET Framework 4.8

# Set the Chinese filename we want for the final output
$ChineseFileName = "鼠标键盘操作模拟器.exe"
# Ensure proper encoding for Chinese characters
$OutputEncoding = New-Object -typename System.Text.UTF8Encoding

Write-Host "Merging assemblies into single executable..." -ForegroundColor Green
Write-Host "This tool will generate a single exe file compatible with Windows 7 and .NET Framework 4.8" -ForegroundColor Yellow
Write-Host ""

Write-Host "Building project in Release mode..." -ForegroundColor Green
# Call build.bat with Release parameter
& ".\build.bat" Release

if ($LASTEXITCODE -ne 0) {
    Write-Host "Error: Failed to build project in Release mode" -ForegroundColor Red
    Pause
    exit 1
}

Write-Host "Checking required files..." -ForegroundColor Green

# Check if main program file exists
if (-not (Test-Path "MouseKeyboardOperationSimulator\bin\Release\MouseKeyboardOperationSimulator.exe")) {
    Write-Host "Error: Main program file MouseKeyboardOperationSimulator\bin\Release\MouseKeyboardOperationSimulator.exe not found" -ForegroundColor Red
    Write-Host "Please compile the project first, then run this script." -ForegroundColor Yellow
    Pause
    exit 1
}

# Check if dependency file exists
if (-not (Test-Path "MouseKeyboardOperationSimulator\bin\Release\Newtonsoft.Json.dll")) {
    Write-Host "Error: Dependency file MouseKeyboardOperationSimulator\bin\Release\Newtonsoft.Json.dll not found" -ForegroundColor Red
    Write-Host "Please ensure the project has been compiled and all necessary files are generated." -ForegroundColor Yellow
    Pause
    exit 1
}

# Check if ILMerge tool exists
if (-not (Test-Path "packages\ILMerge.3.0.40\tools\net452\ilmerge.exe")) {
    Write-Host "Error: ILMerge tool packages\ILMerge.3.0.40\tools\net452\ilmerge.exe not found" -ForegroundColor Red
    Write-Host "Please ensure ILMerge package has been installed via NuGet." -ForegroundColor Yellow
    Pause
    exit 1
}

Write-Host "Creating output directory..." -ForegroundColor Green
New-Item -ItemType Directory -Path "Merged" -Force | Out-Null

# Clear the Merged directory before merging
Write-Host "Clearing Merged directory..." -ForegroundColor Green
Get-ChildItem -Path "Merged" -Force | Remove-Item -Recurse -Force

# Use a temporary ASCII-safe filename for ILMerge
$TempFileName = "TempMergedApp.exe"
Write-Host "Merging files..." -ForegroundColor Green
& "packages\ILMerge.3.0.40\tools\net452\ilmerge.exe" /target:winexe /out:"Merged\$TempFileName" "MouseKeyboardOperationSimulator\bin\Release\MouseKeyboardOperationSimulator.exe" "MouseKeyboardOperationSimulator\bin\Release\Newtonsoft.Json.dll" /targetplatform:v4,"C:\Windows\Microsoft.NET\Framework\v4.0.30319"

if ($LASTEXITCODE -eq 0) {
    Write-Host ""
    Write-Host "Successfully merged into temporary file!" -ForegroundColor Green
    Write-Host "Now renaming to Chinese filename..." -ForegroundColor Yellow
    
    # Rename the temporary file to the Chinese filename
    $TempFilePath = "Merged\$TempFileName"
    $FinalFilePath = "Merged\$ChineseFileName"
    
    # Remove existing file if it exists
    if (Test-Path $FinalFilePath) {
        Remove-Item $FinalFilePath -Force
    }
    
    # Rename the temporary file
    try {
        Rename-Item -Path $TempFilePath -NewName $ChineseFileName
        Write-Host ""
        Write-Host "Successfully renamed to: Merged\$ChineseFileName" -ForegroundColor Green
        
        # Clean up temporary files (if any)
        $TempPdbFile = "Merged\TempMergedApp.pdb"
        if (Test-Path $TempPdbFile) {
            Remove-Item $TempPdbFile -Force
        }
        
        # Get file size
        $FileSize = (Get-Item $FinalFilePath).Length / 1KB
        Write-Host "File size: $($FileSize.ToString("F1")) KB" -ForegroundColor Cyan
        
        Write-Host ""
        Write-Host "Important:" -ForegroundColor Yellow
        Write-Host "The generated single-file exe is compatible with Windows 7 systems (requires .NET Framework 4.8)" -ForegroundColor Yellow
        Write-Host "You can distribute this file to other users." -ForegroundColor Yellow
    } catch {
        Write-Host ""
        Write-Host "Error: Failed to rename file to Chinese filename." -ForegroundColor Red
        Write-Host "The merged file is available as: $TempFilePath" -ForegroundColor Yellow
        Write-Host "You can manually rename it to: $ChineseFileName" -ForegroundColor Yellow
        Write-Host "Error details: $_" -ForegroundColor Red
        Pause
        exit 1
    }
} else {
    Write-Host ""
    Write-Host "Failed to merge assemblies." -ForegroundColor Red
    Write-Host "Please check the error message above." -ForegroundColor Red
    Pause
    exit 1
}